﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using SmartMD.DataModel.Entity;
using SmartMD.DataAccess.Service;
using SmartMD.Manager.Common;
using SmartMD.DataModel;
using SmartMD.Manager.Report;

namespace SmartMD.Manager.Specimens.Query
{
    /// <summary>
    /// Interaction logic for SpecUsageDialog.xaml
    /// </summary>
    public partial class SpecUsageDialog : Window
    {
       // private SpecUsage _usage = null;
        private QuerySpecPanel _owner = null;

        public SpecUsageDialog(QuerySpecPanel owner = null)
        {
            InitializeComponent();

            _owner = owner;
        }

        private void BtnOk_Click(object sender, RoutedEventArgs e)
        {
            SpecUsage usage = new SpecUsage();
            usage.Id = System.Guid.NewGuid().ToString();
            usage.User = this.txtUser.Text;
            usage.UseTime = DateTime.Parse(this.txtUseTime.Text);
            usage.Purpose = this.txtPurpose.Text;

           bool success = DataServiceManager.SpecRecordService.SaveSpecUsage(usage);
           List<EntityUsageMapping> mappingList = null;
           EntityUsageMapping mapping = null;
           List<SpecQueryResult> specQueryResults = this._owner.ucSpecQueryList.printListView;
           if (specQueryResults != null && specQueryResults.Count > 0)
           {
               mappingList = new List<EntityUsageMapping>();
               for (int j = 0; j < specQueryResults.Count; j++)
               {
                   mapping = new EntityUsageMapping();
                   mapping.Id = System.Guid.NewGuid().ToString();
                   mapping.EntityId = specQueryResults[j].Id;
                   mapping.RecordId = usage.Id;
                   mappingList.Add(mapping);
               }
              DataServiceManager.SpecRecordService.SaveEntityUsageMappings(mappingList);
              string outWay = (this.txtOutWay.SelectedItem as ComboBoxValueItem).Value.ToString();
              List<SpecEntity> specEntities = new List<SpecEntity>();
               SpecEntity entity = new SpecEntity();
              for (int i = 0; i < specQueryResults.Count; i++)
              {
                  entity = new SpecEntity();
                  entity.Id = specQueryResults[i].Id;
                  specEntities.Add(entity);
              }
              DataServiceManager.SpecimensService.SetSpecState(specEntities, outWay);
              if (success)
              {
                  this.Close();
                  //打印出库表单
                  PrintOutLibraryForm(specQueryResults);
              }
           }           
        }
        /**
         * 功能：打印出库表单
         * 
         * */
        private void PrintOutLibraryForm(List<SpecQueryResult> items)
        {
            PrintListView printListView1 = new PrintListView();

            //创建标题
            printListView1.Columns.Add("标本编码");
            printListView1.Columns.Add("所属患者");
            printListView1.Columns.Add("所属课题");
            printListView1.Columns.Add("标本位置");
            printListView1.Columns.Add("随访人");
            printListView1.Columns.Add("随访周期");
            printListView1.Columns.Add("随访时间");
            printListView1.Columns.Add("标本类型");
            printListView1.Columns.Add("冻融次数");
            printListView1.Columns.Add("标本总量");

            printListView1.Columns[0].Width = 110;
            printListView1.Columns[1].Width = 110;
            printListView1.Columns[2].Width = 110;
            printListView1.Columns[3].Width = 110;
            printListView1.Columns[4].Width = 110;
            printListView1.Columns[5].Width = 110;
            printListView1.Columns[6].Width = 110;
            printListView1.Columns[7].Width = 110;
            printListView1.Columns[8].Width = 110;
            printListView1.Columns[9].Width = 110;

            System.Windows.Forms.ImageList imageList1 = new System.Windows.Forms.ImageList();

            //ListView顯示方式
            printListView1.View = System.Windows.Forms.View.Details;
            printListView1.LineSpace = 15;
            printListView1.BeginUpdate();

            int i = 1;
            foreach (Specimens.SpecQueryResult sqr in items)
            {
                printListView1.Items.Add(i.ToString(), sqr.SpecCode, 0);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.PatientName);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.Subject);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.SiteCode);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowerUper);
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowUpTimes.ToString());
                printListView1.Items[i.ToString()].SubItems.Add(sqr.FollowUpDate.ToString());
                printListView1.Items[i.ToString()].SubItems.Add(sqr.SpecType);
                printListView1.Items[i.ToString()].SubItems.Add("");
                printListView1.Items[i.ToString()].SubItems.Add("");
                i++;
            }

            printListView1.EndUpdate();
            printListView1.DoPrint();
        }

        private void BtnCacel_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void txtOutWay_Initialized(object sender, EventArgs e)
        {
            List<ComboBoxValueItem> datasource = new List<ComboBoxValueItem>();
            datasource.Add(new ComboBoxValueItem { Name = "借出", Value = "LendOut" });
            datasource.Add(new ComboBoxValueItem { Name = "出库（永久）", Value = "OutLibrary" });

            (sender as ComboBox).ItemsSource = datasource;
        }
    }
}
